//
//  TabbarButton.m
//  K8彩票
//
//  Created by zhoubo on 2017/6/6.
//  Copyright © 2017年 zhoubo. All rights reserved.
//

#import "TabbarButton.h"
@interface TabbarButton()
@property(nonatomic,assign) NSInteger fontsize;

@property(nonatomic,strong) UIColor* textColor;

@end

@implementation TabbarButton

+(instancetype)buttonWithFontsize:(NSInteger)fontsize andTextColor:(UIColor*)color
{
    TabbarButton* btn = [[TabbarButton alloc]init];
    
    btn.fontsize = fontsize;
    btn.textColor = color;
    [btn setup];
    
    
    return btn;
    

}

-(instancetype)initWithCoder:(NSCoder *)aDecoder
{
    if (self = [super initWithCoder:aDecoder]) {
        
        [self setup];
    }
    return self;
}

-(instancetype)initWithFrame:(CGRect)frame
{
    if ( self = [super initWithFrame:frame]) {
        [self setup];
        
    }
    return self;
}

-(void)setup
{
    
    if (self.fontsize == 0) {
        self.fontsize = 11;
    }
    if (self.textColor == nil) {
        self.textColor = [UIColor grayColor];
    }
    // 设置文字的对齐方式和文字普通状态高亮状态的颜色
    [self.titleLabel setTextAlignment:NSTextAlignmentCenter];
    [self setTitleColor:self.textColor forState:UIControlStateNormal];
    [self setTitleColor:[UIColor colorWithRed:87/255.0 green:153/255.0 blue:251/255.0 alpha:1] forState:UIControlStateSelected];
    self.imageView.contentMode = UIViewContentModeScaleAspectFit;
    self.titleLabel.font = [UIFont systemFontOfSize:self.fontsize];
    
}

-(void)layoutSubviews
{
    [super layoutSubviews];
    
    CGFloat width = self.frame.size.width;
    CGFloat height = self.frame.size.height;
    
    if (!self.isNeedUp) {
        self.imageView.frame = CGRectMake(width/2 - 12, 5, 24, 24);
        self.titleLabel.frame = CGRectMake(0, height*0.6, width, height*0.4);
    }
    else
    {
        self.imageView.frame = CGRectMake(0,-10,width,height);
    }


}

-(void)setHighlighted:(BOOL)highlighted
{
    
}


-(void)setIsNeedUp:(BOOL)isNeedUp
{
    _isNeedUp = isNeedUp;
    [self addTarget:self action:@selector(btnclick:) forControlEvents:UIControlEventTouchUpInside];
}

-(void)btnclick:(UIButton*)sender
{
    if (self.clickhandle != nil) {
        self.clickhandle();
    }
}

@end
